package com.shengdou.as.slopsapptest.base;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;

import com.shengdou.as.slopsapptest.MyApplication;

/**
 * Created by Think on 2016/7/28.
 *
 * @author huoxiaofei
 */
public class BaseActivity extends AppCompatActivity {
    public static int screenWidth;
    public static int screenHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isNetworkAvailable(this)) {
            selectPermission();//设置网络权限
            return;
        }
        MyApplication application = (MyApplication) this.getApplication();
        application.addActivity(this);
        DisplayMetrics dm = new DisplayMetrics();
        //取得窗口属性
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        //窗口的宽度
        screenWidth = dm.widthPixels;

        //窗口高度
        screenHeight = dm.heightPixels;
    }
    private void selectPermission() {
        new AlertDialog.Builder(this)
                .setMessage("网络无法访问，请检查网络连接")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = null;
                        //判断手机系统的版本  即API大于10 就是3.0或以上版本
                        if (android.os.Build.VERSION.SDK_INT > 10) {
                            intent = new Intent(Settings.ACTION_SETTINGS);
                        } else {
                            intent = new Intent();
                            ComponentName component = new ComponentName("com.android.settings", "");
                            intent.setComponent(component);
                            intent.setAction("android.intent.action.VIEW");
                        }
                        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);//关键的一步，设置启动模式
                        startActivity(intent);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                })
                .create()
                .show();
    }

    //判断网络连接
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
        } else {
            //如果仅仅是用来判断网络连接
            //则可以使用 cm.getActiveNetworkInfo().isAvailable();
            NetworkInfo info = cm.getActiveNetworkInfo();
            /*if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }*/
            if (info == null || !cm.getBackgroundDataSetting()) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
}
